package com.zs.toolbox.web2j.common.config;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.PropertiesConfiguration;

public class WtConfigUtil {
    static Configuration conf;


    /**
     * 该方式是线程安全方法，正确的使用方法是读取成功后，保存到本地，请过度重复使用，影响性能
     *
     * @return 返回系统配置，默认加载classpath://zs.properties配置文件，如需加入其它文件，可调用reload方式进行重新加载后使用
     */
    public synchronized static Configuration conf() {
        if (conf != null) {
            return conf;
        } else {
            reload();
        }
        return conf;
    }

    /**
     * 从classpath中加载最新的配置文件
     */
    private static void reload(String... fileNames) {
        CompositeConfiguration configuration = new CompositeConfiguration();

        try {
            configuration.addConfiguration(new PropertiesConfiguration("wt.properties"));
        } catch (Exception e) {
        }

        try {
            configuration.addConfiguration(new PropertiesConfiguration("application.properties"));
        } catch (Exception e) {
        }


        if (fileNames != null && fileNames.length > 0) {
            for (String fileName : fileNames) {
                try {
                    configuration.addConfiguration(new PropertiesConfiguration(fileName));
                } catch (Exception e) {
                }
            }
        }

        conf = configuration;
    }

    public static void main(String[] args) {
        Configuration conf = conf();
        System.out.println(conf);
    }

}